<?php
namespace Modules\Entity\Library;

use MongoDB\BSON\ObjectID;

class Common
{
    public static function entityMenuTabs($params)
    {
        $di = \Phalcon\Di::getDefault();
        $entity = $params['entity'];
        $contentModelList = $di->getShared('entityManager')->contentModel($entity);
        $menu = [];
        $output = [
            '#templates' => 'menuTabs',
            'title' => '添加',
            'data' => &$menu
        ];
        foreach ($contentModelList as $key => $value) {
            $menu[] = [
                'href' => [
                    'for' => 'adminEntityAdd',
                    'entity' => $entity,
                    'contentModel' => $key
                ],
                'name' => $value['name'],
            ];
        }
        return $output;
    }

    public static function submitFilterForm(\Core\Forms\Form $form)
    {
        $data = $form->getData();
        $options = $form->getUserOptions();
        $query = $options['_query'];

        foreach ($data as $name => $value) {
            if (is_string($value) && $value == 'null') {
                continue;
            }
            if (is_array($value) && array_search('null', $value) !== false) {
                continue;
            }
            if (!$form->has($name)) {
                continue;
            }

            $element = $form->get($name);
            $elementOptions = $element->getUserOptions();
            if (!isset($elementOptions['operators'])) {
                $elementOptions['operators'] = '=';
            }

            switch ($elementOptions['operators']) {
                case '=':
                    $query['conditions'][$name] = $value;
                    break;
                default:
                    if (isset($query['conditions'][$name]) && is_string($query['conditions'][$name])) {
                        $query['conditions'][$name] = [];
                    }
                    if (is_string($value)) {
                        $value = [$value];
                    }
                    $query['conditions'][$name][$elementOptions['operators']] = $value;
                    break;
            }

        }

        return $query;
    }

    public static function submitHandleForm(\Core\Forms\Form $form)
    {
        $di = \Phalcon\Di::getDefault();
        $data = $form->getData();
        $options = $form->getUserOptions();
        $field = $di->getShared('entityManager')->field($options['_entity']);
        $field = formFieldInit($field);
        $nodeList = $di->getShared('request')->getPost('checkAll');
        $output = ['delete' => 0, 'handle' => 0, 'error' => 0, 'deleteError' => 0];
        $temNodeList = [];
        foreach ($nodeList as $key => $value) {
            $temNodeList[] = new ObjectID($key);
        }
        $query = [
            'conditions' => [
                '_id' => [
                    '$in' => $temNodeList
                ]
            ]
        ];
        $entityList = $di->getShared('entityManager')->find($options['_entity'], $query);
        foreach ($entityList as $item) {
            if (isset($data['delete']) && $data['delete'] == true) {
                if ($item->delete()) {
                    $output['delete']++;
                } else {
                    $output['deleteError']++;
                }
            } else {
                foreach ($data as $key => $value) {
                    if ($value !== 'null' && isset($field[$key])) {
                        $item->{$key} = $value;
                    }

                }
                if ($item->save()) {
                    $output['handle']++;
                } else {
                    $output['error']++;
                }
            }
        }

        $flash = '';
        foreach ($output as $key => $value) {
            if ($value) {
                switch ($key) {
                    case 'error':
                        $flash .= $value . '项内容更改属性失败；';
                        break;
                    case 'handle':
                        $flash .= $value . '项内容更改属性成功；';
                        break;
                    case 'delete':
                        $flash .= $value . '项内容删除成功；';
                        break;
                    case 'deleteError':
                        $flash .= $value . '项内容删除失败';
                        break;
                }
            }
        }
        if ($flash) {
            $di->getShared('flash')->notice($flash);
        }

        return $form;
    }
}